﻿using Artech.Common;
using Microsoft.Practices.Unity.Utility;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace Artech.Core.Resources
{
    [DataContract(Namespace = Constants.Namespaces.DataContract)]
    public class StringResourceEntry : StatefulEntry
    {
        [DataMember]
        private string culture;

        [DataMember]
        public string Id { get; private set; }
        [DataMember]
        public string Category { get; private set; }
        [DataMember]
        public string Name { get; private set; }
        [DataMember]
        public string Value { get;  set; }

        public CultureInfo Culture { get; private set; }

        [OnSerializing]
        public void OnSerializing(StreamingContext context)
        {
            culture = this.Culture.Name;
        }

        [OnDeserialized]
        public void OnDeserialized(StreamingContext context)
        {
            this.Culture = new CultureInfo(culture);
        }

        private StringResourceEntry()
        { }

        public static StringResourceEntry NewResoureEntry(string category, string name, string value, CultureInfo culture = null)
        {
            return NewResoureEntry(Guid.NewGuid().ToString(), category, name, value, culture);
        }

        public static StringResourceEntry NewResoureEntry(string id, string category, string name, string value, CultureInfo culture = null)
        {
            Guard.ArgumentNotNullOrEmpty(id, "id");
            Guard.ArgumentNotNullOrEmpty(category, "category");
            Guard.ArgumentNotNullOrEmpty(name, "name");
            Guard.ArgumentNotNullOrEmpty(value, "value");

            StringResourceEntry entry = new StringResourceEntry
            {
                Id = id,
                Category = category,
                Name = name,
                Value = value,
                Culture = culture
            };

            entry.AuditInfo.VersionNo = 1;
            return entry;
        }
    }
}